open_clientfd {socket} {connect}
- 0017 C π
- open_listenfd {socket}{bind}{listen}
- Socket Programming C API
- 11. Network Programming {CSAPP}
description
ν΄λΌμ΄μΈνΈκ° μλ²μκ² λ©μμ§λ₯Ό μ λ¬νκΈ° μν΄ νμν κ³Όμ μ€ socket, connect λΆλΆμ λννλ€. getaddrinfo(3)μ hint μΈμμ μ΄λ€ νλκ·Έλ₯Ό λ€λμ§ μμ£Όλ‘ μ½μ΄λ³΄μ.
/*
* open_clientfd - Open connection to server at <hostname, port> and
* return a socket descriptor ready for reading and writing. This
* function is reentrant and protocol-independent.
*
* On error, returns:
* -2 for getaddrinfo error
* -1 with errno set for other errors.
*/
int open_clientfd(char *hostname, char *port) {
int clientfd, rc;
struct addrinfo hints, *listp, *p;
/* Get a list of potential server addresses */
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_socktype = SOCK_STREAM; /* Open a connection */
hints.ai_flags = AI_NUMERICSERV; /* ... using a numeric port arg. */
hints.ai_flags |= AI_ADDRCONFIG; /* Recommended for connections */
if ((rc = getaddrinfo(hostname, port, &hints, &listp)) != 0) {
fprintf(stderr, "getaddrinfo failed (%s:%s): %s\n", hostname, port,
gai_strerror(rc));
return -2;
}
/* Walk the list for one that we can successfully connect to */
for (p = listp; p; p = p->ai_next) {
/* Create a socket descriptor */
if ((clientfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0)
continue; /* Socket failed, try the next */
/* Connect to the server */
if (connect(clientfd, p->ai_addr, p->ai_addrlen) != -1) break; /* Success */
if (close(clientfd) < 0) {
/* Connect failed, try another */ // line:netp:openclientfd:closefd
fprintf(stderr, "open_clientfd: close failed: %s\n", strerror(errno));
return -1;
}
}
/* Clean up */
freeaddrinfo(listp);
if (!p) /* All connects failed */
return -1;
else /* The last connect succeeded */
return clientfd;
}
flags
AI_NUMERICSERV
:service
μΈμμ λ€μ΄κ° ν¬νΈκ° 10μ§μ μ«μμμ μλ €μ£Όλ νλκ·Έ.AI_ADDRCONFIG
: IPv4 addresses are returned in the list pointed to byres
only if the local system has at least one IPv4 address configured, and IPv6 addresses are returned only if the local system has at least one IPv6 address configured.- μ½κ² λ§ν΄ IPv4 μλΉμ€λ§ λ³΄κ³ μΆμ κ²½μ° νν°λ§μ ν΄μ€λ€κ³ νλ€.